import os

# 获取当前目录内容
files = os.listdir('.')

# 写入文件
try:
    with open('dir_list.txt', 'w', encoding='utf-8') as f:
        f.write('当前目录内容:\n')
        for file in files:
            f.write(f'{file}\n')
    print('目录列表已写入dir_list.txt')
except Exception as e:
    print(f'写入失败: {e}')